from django.db import models
from basemodel import BaseBean
from tinymce.models import HTMLField


# Create your models here.
# 帖子基类
class BasePost(BaseBean):
    title = models.CharField(max_length=60, verbose_name="标题")
    user_id = models.ForeignKey("usercenter.UserBean", verbose_name="发布用户", on_delete=models.CASCADE)
    board_id = models.ForeignKey("board", verbose_name="所属板块", on_delete=models.CASCADE)
    topic_id = models.ForeignKey("topic", verbose_name="所属话题", on_delete=models.CASCADE, null=True, blank=True)
    access_num = models.IntegerField(default=0, verbose_name="浏览量")

    def __str__(self):
        return self.title

    class Meta:
        abstract = True


# 文字帖子
class TextPost(BasePost):
    # 富文本内容
    many_text = HTMLField(verbose_name="富文本内容")

    class Meta:
        # 确定表名
        db_table = "table_text_post"
        # 中文名称
        verbose_name = "文字帖子"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 图片帖子
class ImagePost(BasePost):
    # 图片介绍
    text = models.TextField(verbose_name="图片介绍")
    # 图片
    image = models.ImageField(upload_to="post_images", verbose_name="图片")

    class Meta:
        # 确定表名
        db_table = "table_img_post"
        # 中文名称
        verbose_name = "图片帖子"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 用户帖子模型
class UserPostRelations(BaseBean):
    """
    一个帖子可以被多个用户收藏、点赞
    一个用户可以收藏、点赞多个帖子
    """
    # 帖子
    post = models.ForeignKey("BasePost", on_delete=models.CASCADE, verbose_name="帖子标题")
    # 用户
    user = models.ForeignKey("usercenter.UserBean", on_delete=models.CASCADE, verbose_name="用户")
    # 是否被收藏
    is_collection = models.BooleanField(default=0, verbose_name="是否被收藏")
    # 是否被点赞
    is_praise = models.BooleanField(default=0, verbose_name="是否被点赞")

    class Meta:
        # 确定表名
        db_table = "table_post_relations"
        # 中文名称
        verbose_name = "用户帖子关系"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 用户评论模型
class UserCommentRelations(BaseBean):
    """
    一条评论可以被多个用户收藏、点赞
    一个用户可以收藏、点赞多个评论
    """
    # 帖子
    comment = models.ForeignKey("Comments", on_delete=models.CASCADE, verbose_name="评论")
    # 用户
    user = models.ForeignKey("usercenter.UserBean", on_delete=models.CASCADE, verbose_name="用户")
    # 是否被收藏
    is_collection = models.BooleanField(default=0, verbose_name="是否被收藏")
    # 是否被点赞
    is_praise = models.BooleanField(default=0, verbose_name="是否被点赞")

    class Meta:
        # 确定表名
        db_table = "table_post_relations"
        # 中文名称
        verbose_name = "用户评论关系"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 评论
class Comments(BaseBean):
    # 用户
    user = models.ForeignKey("usercenter.UserBean", on_delete=models.CASCADE, verbose_name="用户名")
    # 所属帖子
    post = models.ForeignKey("BasePost", on_delete=models.CASCADE, verbose_name="所属帖子")
    # 是否被点赞
    is_praise = models.BooleanField(default=0, verbose_name="是否被点赞")
    # 评论内容
    content = models.CharField(max_length=2000, verbose_name="评论")

    def __str__(self):
        return self.content

    class Meta:
        # 确定表名
        db_table = "table_comments"
        # 中文名称
        verbose_name = "评论"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 回复
class ReplyBean(BaseBean):
    # 回复的用户名
    user = models.ForeignKey("usercenter.UserBean", on_delete=models.CASCADE, verbose_name="用户名")
    # 评论
    comment = models.ForeignKey("Comments", on_delete=models.CASCADE, verbose_name="评论内容")
    # 所属帖子
    post = models.ForeignKey("BasePost", on_delete=models.CASCADE, verbose_name="所属帖子")
    # 回复内容
    content = models.CharField(max_length=2000, verbose_name="回复内容")
    # # 回复id
    # reply_id = models.ForeignKey("ReplyBean", default=None, null=True, blank=True, on_delete=models.CASCADE,
    #                              verbose_name="回复")


# 版块
class Board(BaseBean):
    # 版块名
    name = models.CharField(max_length=30, verbose_name="版块名")

    def __str__(self):
        return self.name

    class Meta:
        # 确定表名
        db_table = "table_board"
        # 中文名称
        verbose_name = "版块"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 话题
class Topic(BaseBean):
    # 话题名
    name = models.CharField(max_length=30, verbose_name="话题名")
    # 版块
    board = models.ForeignKey("Board", on_delete=models.CASCADE, verbose_name="所属版块")
    # logo
    logo = models.ImageField(upload_to="logo", verbose_name="logo")
    # 简介
    tags = models.CharField(max_length=200, verbose_name="简介")

    def __str__(self):
        return self.name

    class Meta:
        # 确定表名
        db_table = "table_topic"
        # 中文名称
        verbose_name = "话题"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name


# 话题-帖子关系表
class TopicPostRelations(BaseBean):
    """
    一个帖子可以有多个话题，
    一个话题有多个帖子
    """
    # 话题
    topic = models.ForeignKey("Board", on_delete=models.CASCADE, verbose_name="话题")
    # 帖子
    post = models.ForeignKey("BasePost", on_delete=models.CASCADE, verbose_name="所属帖子")

    class Meta:
        # 确定表名
        db_table = "table_topic_post_relations"
        # 中文名称
        verbose_name = "话题帖子关系"
        # 模型的复数形式(即名字后面自动加s)
        verbose_name_plural = verbose_name
